package cn.karent.adapter;

import cn.karent.client.api.CookApi;
import cn.karent.client.dto.*;
import cn.karent.common.Pageable;
import cn.karent.common.Result;
import cn.karent.domain.Cook;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wanshengdao
 * @date 2025/4/15
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CookController {
    
    private final CookApi cookApi;

    @GetMapping("/menus")
    public Result<List<CategoryCook>> list() {
        return cookApi.list();
    }

    @GetMapping("/cook/{id}")
    public Result<Cook> get(@PathVariable("id") Integer id) {
        return cookApi.get(id);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody @Valid DeleteCookReq req) {
        cookApi.delete(req.getCookId());
        return Result.ok();
    }

    @PostMapping("/publish")
    public Result<String> publish(@RequestBody @Valid PublishReq req) {
        cookApi.publish(req.getCategory(), req.getName(), req.getContent());
        return Result.ok();
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody @Valid CookUpdateReq req) {
        cookApi.update(req.getId(), req.getCategory(), req.getName(), req.getContent());
        return Result.ok();
    }

    @PostMapping("/search")
    public Pageable<SearchResp> search(@RequestBody @Valid SearchReq req) {
        return cookApi.search(req.getValue(), req.getPage(), req.getNum());
    }


}
